package lili.com.my.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import lili.com.my.R;

public class RegisterActivity extends AppCompatActivity {

    private EditText musername;
    private EditText mpassword;
    private EditText again;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        musername = (EditText) findViewById(R.id.et_user_name);
        mpassword = (EditText) findViewById(R.id.et_user_psw);
        again = (EditText) findViewById(R.id.et_user_again);
    }
    public void register(View view){
        if (!mpassword.getText().toString().equals(again.getText().toString())){
            Toast.makeText(RegisterActivity.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(musername.getText().toString())){
            Toast.makeText(RegisterActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(mpassword.getText().toString())){
            Toast.makeText(RegisterActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Register");

        params.addQueryStringParameter("username", musername.getText().toString());
        params.addQueryStringParameter("password", mpassword.getText().toString());
        x.http().get(params, new Callback.CommonCallback<String>() {

            public void onSuccess(String result) {
                Gson gson=new Gson();
                LoginModel loginModel=gson.fromJson(result,LoginModel.class);
                if (loginModel.code==200){
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(RegisterActivity.this,"注册失败"+loginModel.msg,Toast.LENGTH_SHORT).show();
                }
                 Log.i("RegisterActivity","onSuccess code:"+loginModel.code);
            }

            public void onError(Throwable ex, boolean isOnCallback) {

            }
            public void onCancelled(CancelledException cex) {

            }

            public void onFinished() {

            }
        });
    }
    public void back(View view){
        finish();
    }
    }

